package com.koron.css2.newProjectCenter.impl;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.koron.css2.ServerInterface;
import com.koron.css2.newProjectCenter.bean.PjPubUpMaterial;
import com.koron.css2.newProjectCenter.mapper.PjPubUpMaterialMapper;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author: 苏小龙
 * @CreateTime: 2024-12-11
 * @Description: 材料信息多个更新
 * @Version: 1.0
 */
public class PjPubUpMaterialMoreUpdate implements ServerInterface {
    @Override
    public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
        try {
            String rr=JsonUtils.objectToJson(req.getData());
            Map<String, Object> initDatas = JSON.parseObject(rr);
            String data = JSONObject.toJSONString(initDatas.get("list"));
            ArrayList<PjPubUpMaterial> list = JSON.parseObject(data, ArrayList.class);
            List<PjPubUpMaterial> converList = BeanUtil.copyToList(list, PjPubUpMaterial.class);
            PjPubUpMaterialMapper pjPubUpInvoiceMapper= factory.getMapper(PjPubUpMaterialMapper.class);
            if(list!=null&&list.size()>0){
                for (PjPubUpMaterial pubUpInvoice:converList) {
                    // 1. updateTime,updateAccount,updateName
                    pubUpInvoice.setUpdateInfo(userInfo);
                    // 2. 更新数据
                    pjPubUpInvoiceMapper.update(pubUpInvoice);
                }
            }
            // 3. 返回结果
            return MessageBean.create(Constant.MESSAGE_INT_SUCCESS,"材料信息更新成功",null);
        } catch (Exception e) {
            logger.error("材料信息更新失败",e);
            return MessageBean.create(Constant.MESSAGE_INT_FAIL,"材料信息更新失败",null);
        }
    }
}
